package com.guigu.eduservice.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.eduservice.dto.LoginResponseDto;
import com.guigu.common.utils.R;
import com.guigu.eduservice.dto.OperatorDto;
import com.guigu.eduservice.entity.Operator;
import com.guigu.eduservice.service.OperatorService;
import com.guigu.eduservice.utils.MD5;
import com.guigu.eduservice.vo.OperatorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 操作员表（平台内部员工账号，如管理员、客服） 前端控制器
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/eduservice/operator")
public class OperatorController {

    @Autowired
    private OperatorService operatorService;

    @PostMapping("login")
    public LoginResponseDto login(@RequestBody OperatorDto dto) {
        return operatorService.login(dto);
    }


    //查询所有的操作员
    @PostMapping("/getOperatorAll")
    public R getOperatorAll(){
        List<Operator> list = operatorService.list();
        return  R.ok().data("list",list);

    }


    //分页查询操作员
    @PostMapping("/getOperatorAllPage/{PageNo}/{PageSize}")
    public R getOperatorAllPage(@PathVariable("PageNo") Integer PageNo,
                                @PathVariable("PageSize")Integer PageSize,
                                @RequestBody OperatorVo vo){
        PageInfo<Operator> operatorAllPage = operatorService.getOperatorAllPage(PageNo, PageSize, vo);

        return  R.ok().data("list",operatorAllPage);
    }

    //修改操作员
    @PutMapping
    public R UpdateOperstor(@RequestBody Operator operator){
        operator.setUpdateTime(LocalDateTime.now());
        boolean b = operatorService.updateById(operator);
        if(b){
         return    R.ok().data("ok","修改成功");
        }
        return    R.ok().data("no","修改失败");
    }


    //根据Id查询操作员
    @GetMapping("/{id}")
    public R getOperatorById(@PathVariable("id") Integer id){
        Operator byId = operatorService.getById(id);
        return R.ok().data("Operator",byId);
    }


    //添加操作员
    @PostMapping
    public R AddOperator(@RequestBody Operator operator){
        operator.setPassword(MD5.encrypt(operator.getPassword()));
        boolean save = operatorService.save(operator);
        if(save){
            return    R.ok().data("ok","添加成功");
        }
        return    R.ok().data("no","添加失败");
    }

    //删除操作员
    @DeleteMapping("/{id}")
    public R DeleteOperator(@PathVariable("id") Integer id){
        boolean b = operatorService.removeById(id);
        if(b){
            return    R.ok().data("ok","删除成功");
        }
        return    R.ok().data("no","删除失败");
    }
}
